Return to start page

Core/String/Library Conversion.j

Code

		
1			library ALibraryCoreStringConversion requires ALibraryCoreStringMisc
2
3 /**
4 * Converts an ASCII char to an integer.
5 * ASCII is the American Keyboard Standard.
6 * Conversion of a char to ASCII works with the integer type: 'a' = 97.
7 * @author Peppar
8 * @source http://www.wc3jass.com/
9 * @todo Syntax error?!
10 */
11 function AsciiToChar takes integer i returns string
12 if (i == 0) then
13 return null
14 elseif ((i >= 8) and (i <= 10)) then
15 return SubString("\b\t\n", i - 8, i - 7)
16 elseif ((i >= 12) and (i <= 13)) then
17 return SubString("\f\r", i - 12, i - 11)
18 elseif ((i >= 32) and (i <= 127)) then
19 return SubString(" !\"#$%%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~", i - 32, i - 31) //syntax error?
20 endif
21 return ""
22 endfunction
23
24 /**
25 * Converts a specific number of seconds to a string with minutes and hours.
26 * Example: 120 seconds - 02:00.
27 */
28 function GetTimeString takes integer seconds returns string
29 local string result = ""
30 local integer minutes = seconds / 60
31 local integer hours = minutes / 60
32 set seconds = ModuloInteger(seconds, 60)
33 set minutes = ModuloInteger(minutes, 60)
34 if (seconds >= 10) then
35 set result = I2S(seconds)
36 else
37 set result = "0" + I2S(seconds)
38 endif
39 set result = result + ":"
40 if (minutes >= 10) then
41 set result = result + I2S(minutes)
42 else
43 set result = result + "0" + I2S(minutes)
44 endif
45 set result = result + ":"
46 if (hours >= 10) then
47 set result = result + I2S(hours)
48 else
49 set result = result + "0" + I2S(hours)
50 endif
51 return result
52 endfunction
53
54 /// Converts string @param colorString to a player color.
55 function StringToPlayerColor takes string colorString returns playercolor
56 if (colorString == "ff0000") then
57 return PLAYER_COLOR_RED
58 elseif (colorString == "0000ff") then
59 return PLAYER_COLOR_BLUE
60 elseif (colorString == "1CB619") then
61 return PLAYER_COLOR_CYAN
62 elseif (colorString == "800080") then
63 return PLAYER_COLOR_PURPLE
64 elseif (colorString == "ffff00") then
65 return PLAYER_COLOR_YELLOW
66 elseif (colorString == "ff8000") then
67 return PLAYER_COLOR_ORANGE
68 elseif (colorString == "00ff00") then
69 return PLAYER_COLOR_GREEN
70 elseif (colorString == "ff80C0") then
71 return PLAYER_COLOR_PINK
72 elseif (colorString == "C0C0C0") then
73 return PLAYER_COLOR_LIGHT_GRAY
74 elseif (colorString == "0080ff") then
75 return PLAYER_COLOR_LIGHT_BLUE
76 elseif (colorString == "106246") then
77 return PLAYER_COLOR_AQUA
78 elseif (colorString == "804000") then
79 return PLAYER_COLOR_BROWN
80 endif
81 return null
82 endfunction
83
84 /// Converts player color @param playerColor to a string.
85 function PlayerColorToString takes playercolor playerColor returns string
86 if (playerColor == PLAYER_COLOR_RED) then
87 return "ff0000"
88 elseif (playerColor == PLAYER_COLOR_BLUE) then
89 return "0000ff"
90 elseif (playerColor == PLAYER_COLOR_CYAN) then
91 return "1CB619"
92 elseif (playerColor == PLAYER_COLOR_PURPLE) then
93 return "800080"
94 elseif (playerColor == PLAYER_COLOR_YELLOW) then
95 return "ffff00"
96 elseif (playerColor == PLAYER_COLOR_ORANGE) then
97 return "ff8000"
98 elseif (playerColor == PLAYER_COLOR_GREEN) then
99 return "00ff00"
100 elseif (playerColor == PLAYER_COLOR_PINK) then
101 return "ff80c0"
102 elseif (playerColor == PLAYER_COLOR_LIGHT_GRAY) then
103 return "c0c0c0"
104 elseif (playerColor == PLAYER_COLOR_LIGHT_BLUE) then
105 return "0080ff"
106 elseif (playerColor == PLAYER_COLOR_AQUA) then
107 return "106246"
108 elseif (playerColor == PLAYER_COLOR_BROWN) then
109 return "804000"
110 endif
111 return "ffffff"
112 endfunction
113
114 /**
115 * I changed the function and called my own string functions.
116 * @author Peppar
117 * @link http://www.wc3jass.com/
118 */
119 function HighlightShortcut takes string whichString, integer shortcut, string colour returns string
120 local string result
121 local string newShortcut = AsciiToChar(shortcut)
122 local integer shortcutPosition = FindString(whichString, newShortcut)
123 if (shortcutPosition != -1) then
124 if (colour == null) then
125 set colour = "ffffcc00"
126 endif
127 set result = InsertString(whichString, shortcutPosition + 1, "|r")
128 set result = InsertString(whichString, shortcutPosition, ("|c" + colour))
129 return result
130 endif
131 return whichString
132 endfunction
133
134 /**
135 * New argument function.
136 * Is much faster because you don't have to filter all the arguments and don't have to convert the types by yourself.
137 * @code
138 * StringArg(StringArg(IntegerArg("You're %i years old and you're called %s. Besides you're %s.", 0), "Peter"), "gay")
139 * @endcode
140 */
141 //! textmacro AStringArgumentMacro takes TYPE, TYPENAME, TYPECHAR, CONVERSIONFUNCTION
142 function $TYPENAME$Arg takes string whichString, $TYPE$ value returns string
143 local string result = whichString
144 local integer index = FindString(result, "%$TYPECHAR$")
145 if (index != -1) then
146 set result = RemoveSubString(result, index, 2)
147 set result = InsertString(result, index, $CONVERSIONFUNCTION$(value))
148 endif
149 return result
150 endfunction
151 //! endtextmacro
152
153 //! runtextmacro AStringArgumentMacro("integer", "Integer", "i", "I2S")
154 //! runtextmacro AStringArgumentMacro("real", "Real", "r", "R2S")
155 //! runtextmacro AStringArgumentMacro("string", "String", "s", "")
156
157 /// @param width Width of argument string in characters (if it is too short there will be inserted space characters).
158 function RealArgW takes string whichString, real value, integer width, integer precision returns string
159 local string result = whichString
160 local integer index = FindString(whichString, "%r")
161 if (index != -1) then
162 set result = RemoveSubString(result, index, 2)
163 set result = InsertString(result, index, R2SW(value, width, precision))
164 endif
165 return result
166 endfunction
167
168 /**
169 * @author Extrarius
170 * @link http://www.wc3jass.com/
171 */
172 function GetExternalString takes integer index returns string
173 if (index < 0) then
174 return ""
175 elseif (index < 10) then
176 return GetLocalizedString("TRIGSTR_00" + I2S(index))
177 elseif (index < 100) then
178 return GetLocalizedString("TRIGSTR_0" + I2S(index))
179 else
180 return GetLocalizedString("TRIGSTR_" + I2S(index))
181 endif
182 endfunction
183
184 /**
185 * It is possible to search in the string files of the mpq directory "ui/FrameDef".
186 * There are some general string files.
187 * tr means translation.
188 * @code
189 * native GetLocalizedString takes string source returns string
190 * @endcode
191 */
192 function tr takes string source returns string
193 return GetLocalizedString(source)
194 endfunction
195
196 /**
197 * sc means shortcut.
198 * @code
199 * native GetLocalizedHotkey takes string source returns integer
200 * @endcode
201 */
202 function sc takes string source returns integer
203 return GetLocalizedHotkey(source)
204 endfunction
205
206 /// Not checked yet.
207 /// @todo Bugged?
208 function InsertLineBreaks takes string whichString, integer maxLineLength returns string
209 local integer i
210 local string result = ""
211 if (StringLength(whichString) <= maxLineLength) then
212 return whichString
213 endif
214 set i = maxLineLength
215 loop
216 exitwhen (i > StringLength(whichString))
217 set result = result + SubString(whichString, i - maxLineLength, i) + "|n"
218 set i = i + maxLineLength
219 endloop
220
221 if (i != StringLength(whichString)) then
222 set result = result + SubString(whichString, i - maxLineLength, StringLength(whichString))
223 endif
224
225 return result
226 endfunction
227
228 endlibrary